<?php

namespace app\student\controller;

use app\student\model\classesModel;
use think\Controller;

class Classes extends Controller
{
    //查询班级
    public function selectClasses()
    {
        $selectClasses = classesModel::selectClassesModel();
        $this->assign('selectClasses',$selectClasses);
        return $this->fetch('selectClasses');
    }

    //增加班级
    public function insertClasses()
    {
        if($this->request->isPost()) {
            $insertClasses = $this->request->post();
            //var_dump($insertClasses);die;
            $result = classesModel::insertClassesModel($insertClasses);
            if($result)
            {
                $this->success('增加成功','classes/selectClasses','','1');
            }else{
                $this->error('增加失败','classes/insertClasses','','1');
            }
        }else{
            return $this->fetch('classes/insertClasses');
        }
    }

    //删除班级
    public function deleteClasses()
    {
        if($this->request->isGet())
        {
            $deleteClasses = $this->request->get();
            $result = classesModel::deleteClassesModel($deleteClasses);
            if($result)
            {
                return $this->success('删除成功','classes/selectClasses','','1');
            }else{
                return $this->error('删除失败','classes/selectClasses','','1');
            }
        }else{
            return $this->fetch('classes/selectClasses');
        }

    }

    //修改班级---需要先查询出班级原先状态在进行修改
    public function updateClasses()
    {
        if($this->request->isPost())
        {
            $updateClasses = $this->request->post();
            $result = classesModel::updateClassesModel($updateClasses);
            //var_dump($result);die;
            if($result)
            {
                return $this->success('修改成功','classes/selectClasses','','1');
            }else{

                //这里是重新获取从表单里面传过来的值，并且在一次给视图赋值，防止修改失败返回出错，
                $getUpdate =  $this->request->post();
                //var_dump($getUpdate);die;
                $this->assign('getUpdate',$getUpdate);
                return $this->fetch('classes/updateClasses');
            }

        }else{
                $getUpdate = $this->request->get();
                $result = classesModel::GetClassesModel($getUpdate);
                //var_dump($result);die;
                if($result)
                {
                    $this->assign('getUpdate',$result);
                    return $this->fetch('classes/updateClasses');
                }else{
                    return $this->error('修改失败','classes/selectClasses','','1');
                }
        }

    }
}